package com.usian.service;

import com.usian.mapper.TbContentCategoryMapper;
import com.usian.pojo.TbContentCategory;
import com.usian.pojo.TbContentCategoryExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ContentCategoryService {

    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;

    /**
     * 根据父节点 Id 查询子节点
     */
    public List<TbContentCategory> selectContentCategoryByParentId(Long id) {
        List<TbContentCategory> list = tbContentCategoryMapper.selectContentCategoryByParentId(id);
        return list;
    }

    /**
     * 添加内容分类
     */
    public Integer insertContentCategory(TbContentCategory tbContentCategory) {
       //1、添加内容分类
        Date date = new Date();
        tbContentCategory.setUpdated(date);
        tbContentCategory.setCreated(date);
        tbContentCategory.setIsParent(false);
        //排列序号，表示同级类目的展现次序，如数值相等则按名称次序排列。取值范围:大于零的整数 默认为一
        tbContentCategory.setSortOrder(1);
        Integer i = tbContentCategoryMapper.insertSelective(tbContentCategory);
        //2、如果不是父节点，就改成父节点
        //查询当前新节点的父节点
        TbContentCategory contentCategory = tbContentCategoryMapper.selectByPrimaryKey(tbContentCategory.getParentId());
        //3、判断当前父节点是否还有子节点
        if (!contentCategory.getIsParent()){
             contentCategory.setIsParent(true);
             contentCategory.setUpdated(date);
             contentCategory.setCreated(date);
             tbContentCategoryMapper.updateByPrimaryKey(contentCategory);
        }
            return i;
    }

    /**
     * 删除内容分类
     */
    public Integer deleteContentCategoryById(Long categoryId) {
        //查询当前节点
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(categoryId);
        //父节点不容许删除
        if (tbContentCategory.getIsParent()==true){
            return 0;
        }
        //不是父节点
        tbContentCategoryMapper.deleteByPrimaryKey(categoryId);
        //当前节点的兄弟节点
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = tbContentCategoryExample.createCriteria();
        criteria.andParentIdEqualTo(tbContentCategory.getParentId());
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
        //删除之后如果父节点中没有子节点，则将isparent修改为false
        if(list.size()==0){
            TbContentCategory contentCategory = new TbContentCategory();
            contentCategory.setIsParent(false);
            Date date = new Date();
            contentCategory.setUpdated(date);
            contentCategory.setCreated(date);
            tbContentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
        }
            return 200;
    }


    /**
     * 修改内容分类
     */
    public Integer updateContentCategory(TbContentCategory tbContentCategory) {
        Date date = new Date();
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setUpdated(date);
        tbContentCategory.setCreated(date);
        int i = tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
        return i;
    }
}
